package cn.ldh.xhr_02;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

import java.nio.charset.StandardCharsets;

public class Producer {

    public static void main(String[] args) throws Exception {
        // 1 创建生产者对象
        DefaultMQProducer producer = new DefaultMQProducer("xhr_producer_group");

        // 2 设置nameServer地址
        producer.setNamesrvAddr("localhost:9876");

        // 3 启动
        producer.start();

        //  4 创建消息
        Message message = new Message("xhr_log", "hello", "没有最帅只有更帅！".getBytes(StandardCharsets.UTF_8));

        // 5 发送消息 因为是异步 所以同时也要得到成功和失败的响应
        producer.send(message, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println(sendResult);
            }

            @Override
            public void onException(Throwable throwable) {

                System.out.println("程序异常"+throwable.getMessage());

            }
        });

        //producer.shutdown();



    }
}
